﻿
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Add</title>
    <link href="~/Content/lib/layui/css/layui.css" rel="stylesheet" />
    <script src="~/Content/lib/layui/layui.js"></script>
</head>
<body>
    <form id="fAdd" class="layui-form" action="">
        <div class="layui-form-item">
            <label class="layui-form-label">新闻标题</label>
            <div class="layui-input-inline">
                <input type="text" id="title"  name="title" required lay-verify="required" placeholder="请输入新闻标题" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">新闻内容</label>
            <div class="layui-input-inline">
                <input type="text" id="content" name="content" required lay-verify="required" placeholder="请输入新闻内容" autocomplete="off" class="layui-input">
            </div>
        </div>


        <div class="layui-form-item">
            <label class="layui-form-label">是否显示在首页</label>
            <div class="layui-input-inline">
                <select id="isShowHome" name="isShowHome">
                    <option value=""></option>
                    <option value="1">是</option>
                    <option value="0">否</option>
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">新闻类型</label>
            <div class="layui-input-inline">
                <select id="typeId" name="TypeId">
                    <option value="">---请选择新闻类型---</option>
                    @foreach (var item in Model)
                    {
                        <option value="@item.id">@item.TypeName</option>
                    }
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">图片</label>
            <div class="layui-input-block">
                <input type="file" id="my_file" name="file">
                <input type="text" hidden id="defaultImg" />
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label"></label>
            <div class="layui-input-block">
                <img id="my_img" style="width:200px;height:200px" />
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>

    <script>
        //Demo
        layui.use(['form', 'laydate', 'upload'], function () {
            var form = layui.form;
            var $ = layui.$;
            var upload = layui.upload;
            var laydate = layui.laydate;

            //日期
            laydate.render({
                elem: '#time'
            });
            upload.render({
                elem: "#my_file",
                url: "/Back/Home/UploadImg",
                done: function (res) {
                    if (res.code == 0) {
                        $("#my_img").attr("src", "/images/" + res.path);
                        $("#defaultImg").val(res.path);
                    }
                }
            })
            //监听提交
            form.on('submit(formDemo)', function (data) {
                $.ajax({
                    url: "/Back/News/Add",
                    type: "post",
                    data: {

                        title: $("#title").val(),
                        content: $("#content").val(),
                        isShowHome: $("#isShowHome").val(),
                        typeId: $("#typeId").val(),
                        defaultImg: $("#defaultImg").val(),

                    },
                    dataType: "json",
                    success: function (res) {
                        if (res.suc) {
                            layer.alert(res.msg);
                            //当你在iframe页面关闭自身时
                            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                            parent.layer.close(index); //再执行关闭
                            window.parent.location.reload();
                        } else {
                            layer.alert(res.msg);
                            window.parent.location.reload();
                        }
                    }
                })
                return false;
            });
        });
    </script>
</body>
</html>
